package me.bincker.formwork.web.core.config.jackson

import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider
import com.fasterxml.jackson.datatype.hibernate5.Hibernate5Module
import me.bincker.formwork.web.core.config.jackson.mixin.PageMixin
import org.springframework.context.annotation.Configuration
import org.springframework.data.domain.Page
import javax.persistence.EntityManagerFactory

@Configuration
class JacksonConfiguration(
        objectMapper: ObjectMapper
) {
    init {
        objectMapper.addMixIn(Page::class.java, PageMixin::class.java)
//        设置默认的Filter提供者，并设置当注解在序列化对象上的Filter找不到时不抛出异常
        objectMapper.setFilterProvider(SimpleFilterProvider().also { it.setFailOnUnknownId(false) })

//        序列化对象时如果遇到懒加载的对象是否重新打开会话进行查询获取
        val hibernate5Module = Hibernate5Module()
        hibernate5Module.enable(Hibernate5Module.Feature.FORCE_LAZY_LOADING)
        objectMapper.registerModule(hibernate5Module)
    }
}


